

module Remote

  class Export < Controller

    plug 'export'

    def index e
      <<HTML
<h1>Hello!</h1>

<p>here you can<p>

<h2>Get data</h2>
<pre>
?json={"name" => 'select', "params" => {'filter' => { datamapper-filter-options }}}
</pre>

<h2>Grab images</h2>
<pre>
images?json={"name" => "get", "params" => {"id" => IMAGE_ID }}
images?json={"name":"list","params":{}} <a href="/export/images?json=%7B%22name%22%3A%22list%22%2C%22params%22%3A%7B%7D%7D">try it</a>
</pre>

HTML
    end

    def images e
      ExportImages.go(e)
    end

    def on_select e
      things = []
      if e.kind == 'all'
        things = Thing.all(e.filter)
      end
      if e.kind == 'first'
        if t = Thing.first(e.filter)
          things = [t]
        end
      end
      if e.kind == 'last'
        if t = Thing.last(e.filter)
          things = [t]
        end
      end
      e.respond(:things => things.map(&:state))
    end
  end


  class Layout
    def self.render e
      <<HTML
<html>
<body>
</body>
  #{e.content}
</html>
HTML
    end
  end

  Export.layout Layout

end


=begin
class ExportController < Controller

  plug '_export'

  def dump e
    entities = Thing.all({:identity => e.params['identities'].split("-")})
    entities.to_a.map {|r| r.to_hash }.to_json
  end

  def on_select e
    "["+ImportExport::Export.new.
      select(
        JSON.parse(e.params['filter']), 20
      ).
      map(&:state).join(",")+"]"
  end

  def list e
    Thing.all.to_a.map { |t|
      [
        #t.kind,
        t.identity,
        t.version
        #,t.changed_at,
      ]
    }.to_json
  end

  class Test < FwTest
    def test_dump_one_thing
      DataMapper.setup(:test, :adapter => 'in_memory')
      repository(:test) {
        t = Thing.new
        t.save

        data = (JSON.parse ExportController.new.dump(
            Event.new(nil, :params => {'identities' => t.identity})
          ))

        eq data.length, 1
        eq data[0]['identity'], t.identity
        eq data[0]['version'], t.version
        #eq data[0]['changed_at'], t.changed_at # comparison fails atm
      }
    end

    def test_dump_only_wanted_things
      DataMapper.setup(:test, :adapter => 'in_memory')
      repository(:test) {
        things = (0..4).to_a.map {
          t = Thing.new
          t.save
          t
        }

        to_dump = things[2]

        data = (JSON.parse ExportController.new.dump(
            Event.new(nil, :params => {'identities' => to_dump.identity})
          ))

        eq data.length, 1
        eq data[0]['identity'], to_dump.identity
        eq data[0]['version'], to_dump.version
        #eq data[0]['changed_at'], t.changed_at # comparison fails atm
      }
    end


    def test_dump_only_wanted_things
      DataMapper.setup(:test, :adapter => 'in_memory')
      repository(:test) {
        things = (0..4).to_a.map {
          Thing.new.tap(&:save)
        }

        to_dump = [things[1], things[3], things[4]]

        data = (JSON.parse ExportController.new.dump(
            Event.new(nil,
              :params => {
                'identities' => to_dump.map(&:identity).join("-")
              })
          ))

        eq data.length, 3
        eq data[0]['identity'], to_dump[0].identity
        eq data[0]['version'], to_dump[0].version

        eq data[1]['identity'], to_dump[1].identity
        eq data[1]['version'], to_dump[1].version

        eq data[2]['identity'], to_dump[2].identity
        eq data[2]['version'], to_dump[2].version
        #eq data[0]['changed_at'], t.changed_at # comparison fails atm
      }
    end
  end

end


module ImportExport

  class Export
    def self.serialize thing
      thing.to_json
    end

    def select criteria, limit = 20
      criteria[:limit] = limit
      Thing.all(criteria)
    end
  end


  class TestSelecting < FwTest
    def test_select_returns_objects
      DataMapper.setup(:testing, :adapter => 'in_memory')
      repository(:testing) {
        t = Thing.new
        t.save
        e = Export.new
        eq e.select(:identity => t.identity), [t]
      }
    end

    def test_select_filters_by_give_hash
      DataMapper.setup(:testing, :adapter => 'in_memory')
      repository(:testing) {
        things = (0..2).to_a.map {
          Thing.new.tap &:save
        }
        eq Export.new.select(:identity => things[1].identity), [things[1]]
      }
    end
  end

end
=end